package com.unicom.scg.scancodego.component;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName: LoginHandlerInterceptor <br/>
 * Description: 登录检查拦截器
 * Date: 2019/10/26 13:47<br/>
 *
 * @author jason<br />
 * @version v1.0
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 获取session中保存的参数
        Object loginUser = request.getSession().getAttribute("loginUser");

        if(loginUser == null){
            request.setAttribute("msg","我是拦截器，您没有登录，请登录！");
            // 跳转到登录页,请求转发（包含原来请求中所有的数据）
//            request.getRequestDispatcher("/login").forward(request,response);


            // 重定向请求(不会包含原来请求中所有的数据)
            response.sendRedirect("/login");

//            String path = request.getSession().getServletContext().getContextPath();
//            System.out.println(path);
//            response.sendRedirect(path + "/login");

            return false;
        } else {
            // 放行
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
